//
//  NotificationGroupMapViewController.m
//  MapLink
//
//  Created by student on 5/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NotificationGroupMapViewController.h"
#import "NotificationEntity.h"
#import "MapAnnotation.h"


@implementation NotificationGroupMapViewController
@synthesize notification;

- (id) init {
	[super initWithNibName:@"NotificationGroupMapViewController" bundle:nil];
	[[self navigationItem] setTitle:@"Notification Map"];
	return self;
}

- (id) initWithNotification:(NotificationEntity *)object {
	self = [self init];
	self.notification = object;
	[[self navigationItem] setTitle:self.notification.groupName];
	return self;
}

-(IBAction)cancel {
	[self dismissModalViewControllerAnimated:YES];
}

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

- (MKAnnotationView *)mapView:(MKMapView *)map 
            viewForAnnotation:(id <MKAnnotation>)annotation 
{
    static NSString *AnnotationViewID = @"annotationViewID";
    MKPinAnnotationView* pinView = 
    (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (!pinView)
    {
		// if an existing pin view was not available, create one
        MKPinAnnotationView* customPinView = 
        [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                                         reuseIdentifier:AnnotationViewID] 
         autorelease];                              
        customPinView.pinColor = MKPinAnnotationColorPurple;
        customPinView.animatesDrop = NO;
        customPinView.canShowCallout =YES;
		
		NSString *urlString = [[NSString stringWithFormat:@"http://mybuddies.emiu.net/photo/%@.png",notification.deviceId]
							   stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
		NSURL *url = [NSURL URLWithString:urlString];
		NSData *avatarData = [NSData dataWithContentsOfURL:url];
		
		if (avatarData) {
			customPinView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[self imageWithImage:[UIImage imageWithData:avatarData] scaledToSize:CGSizeMake(32.0f,32.0f)]];
		} else {
			customPinView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]];
		}
        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}

- (void) viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	
	[mapView setDelegate:self];
	
	CLLocationCoordinate2D notificationPoint;
	notificationPoint.latitude = [notification.locationX floatValue]/1e6;
	notificationPoint.longitude = [notification.locationY floatValue]/1e6;
	
	[mapView removeAnnotations:[mapView annotations]];
	
	NSString *notTitle = [NSString stringWithFormat:@"%@",notification.userName];
	
	MapAnnotation *ma = [[MapAnnotation alloc] initWithCoordinate:notificationPoint  title:notTitle];
	ma.subtitle=[NSString stringWithFormat:@"%@",notification.notificationMsg];
	[mapView addAnnotation:ma];
	[mapView selectAnnotation:ma animated:NO];
	[ma release];
	
	MKCoordinateRegion region;
    
	region.center = notificationPoint;
	region.span.latitudeDelta = .005;
	region.span.longitudeDelta = .005;
    
	[mapView setRegion:region animated:TRUE];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	if (notification) [notification release];

    [super dealloc];
}


@end
